<?php

namespace Utuo;

use Think\Template\TagLib;

class Tag extends TagLib {
	// 标签列表
	protected $tags = array(
			// 一键拨号
			'dial' => array(
					'attr' => 'id,debug',
					'close' => 1 
			),
			// 一键导航
			'nav' => array(
					'attr' => 'id',
					'close' => 1 
			),
			// 轮播图
			'slide' => array(
					'attr' => 'id',
					'close' => 1 
			),
			// 轮播图
			'ad' => array(
					'attr' => 'id',
					'close' => 1
			),
			// 轮播图
			'ads' => array(
					'attr' => 'id,cateid',
					'close' => 1
			),
			// 文章栏目列表
			'category' => array(
					'attr' => 'id,pid',
					'close' => 1 
			),
			// 文章列表
			'article' => array(
					'attr' => 'id,cat,limit,order',
					'close' => 1 
			),
			// 链接列表
			'link' => array(
					'attr' => 'id,cat,limit,order',
					'close' => 1
			),
			// 链接列表
			'linklist' => array(
					'attr' => 'name,cateid,limit,order',
					'close' => 1
			),
			// 户型列表
			'houseType' => array(
					'attr' => 'id',
					'close' => 1 
			),
			// 楼盘扩展信息列表
			'houseConfig' => array(
					'attr' => 'id',
					'close' => 1 
			),
			// 场景列表
			'houseScene' => array(
					'attr' => 'id',
					'close' => 1 
			),
			// 房产像册列表
			'housePhoto' => array(
					'attr' => 'id',
					'close' => 1 
			),
			// 图片列表
			'housePhotoImage' => array(
					'attr' => 'id,pt,limit',
					'close' => 1 
			),
			// 轮播图
			'houseSlide' => array(
					'attr' => 'id',
					'close' => 1
			),
			// 一键拨号
			'houseDial' => array(
					'attr' => 'id,debug',
					'close' => 1
			),
			// 一键导航
			'houseNav' => array(
					'attr' => 'id',
					'close' => 1
			),
			// 像册列表
			'photo' => array(
					'attr' => 'id',
					'close' => 1 
			),
			// 图片列表
			'photoImage' => array(
					'attr' => 'id,pt,limit',
					'close' => 1 
			),
			// 微商城信息列表
			'productConfig' => array(
					'attr' => 'id',
					'close' => 1
			),
			// 商品栏目列表
			'productCategory' => array(
					'attr' => 'id,pid,limit',
					'close' => 1 
			),
			// 商品栏目列表
			'productCategorylist' => array(
					'attr' => 'name,pid,limit,order',
					'close' => 1
			),
			//
			// 商品列表
			'productGoods' => array(
					'attr' => 'id,cat,class,is_low,limit,order,child',
					'close' => 1,
			),
			// 轮播图
			'productSlide' => array(
					'attr' => 'id',
					'close' => 1
			),
			// 微商城信息列表
			'shopConfig' => array(
					'attr' => 'id',
					'close' => 1
			),
			// 商品栏目列表
			'shopCategory' => array(
					'attr' => 'id,pid,cats,limit',
					'close' => 1
			),
			// 商品列表
			'shopGoods' => array(
					'attr' => 'id,cat,class,is_low,limit,order,child',
					'close' => 1,
			),
			// 轮播图
			'shopSlide' => array(
					'attr' => 'id',
					'close' => 1
			),
			// 热词
			'shopHotWords' => array(
					'attr' => 'id,limit',
					'close' => 1
			),
			// 品牌推荐
			'shopBrandRecommend' => array(
					'attr' => 'id,limit',
					'close' => 1
			),
			// 还买了
			'shopAlsoBought' => array(
					'attr' => 'id,product_id,order,limit',
					'close' => 1
			),
			// 活动列表
			'activity' => array(
					'attr' => 'id',
					'close' => 1
			),
			// 微商城信息列表
			'disShopConfig' => array(
					'attr' => 'id',
					'close' => 1
			),
    	    // 商品栏目列表
    	    'disShopCategory' => array(
    	        'attr' => 'id,cid,cats,limit',
    	        'close' => 1
    	    ),
    	    // 轮播图
    	    'disShopSlide' => array(
    	        'attr' => 'id',
    	        'close' => 1
    	    ),
    	    // 热词
    	    'disShopHotWords' => array(
    	        'attr' => 'id,limit',
    	        'close' => 1
    	    ),
    	    // 品牌推荐
    	    'disShopBrandRecommend' => array(
    	        'attr' => 'id,limit',
    	        'close' => 1
    	    ),
    	    // 商品列表
    	    'disShopGoods' => array(
    	        'attr' => 'id,cat,class,is_low,limit,order,child',
    	        'close' => 1,
    	    ),
    	    // 广告
    	    'disShopAd' => array(
    	        'attr' => 'id',
    	        'close' => 1,
    	    ),
	);
	
	// 一键拨号
	public function _dial($tag, $content) {
		$tagCode = <<<'PARSE'
			$__tag_site_dial__ = array();
			$__tag_site_dial__[] = M('Dial')->where(array(
											"cid" => $sid,
											"mid" => array("exp", "=".M("Model")->where("tag='Site'")->field("id")->select(false))
										))->find();
PARSE;
		return $this->outTag('__tag_site_dial__', $tag, $content, $tagCode);
	}
	
	// 一键导航
	public function _nav($tag, $content) {
		$tagCode = <<<'CODE'
			$navigate = M('Navigate')->where(array(
											"cid" => $sid,
											"mid" => array("exp", "=".M("Model")->where("tag='Site'")->field("id")->select(false))
										))->find();
			$navigate['url'] = 'http://api.map.baidu.com/marker?location='.$navigate['lat'].','.$navigate['lng'].'&title='.$navigate['title'].'&content='.$navigate['address'].'&output=html';
			$__tag_site_nav__[] = $navigate;
CODE;
		return $this->outTag('__tag_site_nav__', $tag, $content, $tagCode);
	}
	
//	// 轮播图 todo 新浪云上传
//	public function _slide($tag, $content) {
//		$tagCode = <<<'CODE'
//			$slideImages = M('Slide')->where(array(
//				"cid" => $sid,
//				"mid" => array("exp", "=".M("Model")->where("tag='Site'")->field("id")->select(false))
//			))->getField("data");
//			$__tag_slide__ = unserialize($slideImages);
//			foreach($__tag_slide__ as &$v) {
//				$v['img'] = url('Api', 'Image', 'show')."?url=".$v['img'];
//			}
//CODE;
//		return $this->outTag('__tag_slide__', $tag, $content, $tagCode);
//	}


	// 轮播图
	public function _slide($tag, $content) {
		$tagCode = <<<'CODE'
			$slideImages = M('Slide')->where(array(
				"cid" => $sid,
				"mid" => array("exp", "=".M("Model")->where("tag='Site'")->field("id")->select(false))
			))->getField("data");
			$__tag_slide__ = unserialize($slideImages);
			foreach($__tag_slide__ as &$v) {
				$v['img'] = $v['img'];
			}
CODE;
		return $this->outTag('__tag_slide__', $tag, $content, $tagCode);
	}
	
	public function _ads($tag,$content){
		$cateid=$tag['cateid'];
		$limit=$tag['limit'];
		$order=$tag['order']?$tag['order']:'id desc';
		$tagCode = <<<'CODE'
				$__tag_ads__ = M('AdContent')->where(array(
				"article_category_id" =>{cateid}
			))->limit({limit})->order('{order}')->select();
			
CODE;
		$tagValues = array(
				'{cateid}' => $cateid,
				'{limit}'=>$limit,
				'{order}'=>$order
		);
		return $this->outTag('__tag_ads__', $tag, $content, $tagCode,$tagValues);
	}
	
	
	
	
	
	
	public function _ad($tag, $content) {
		$_tag=$tag['tag'];
		$limit=$tag['limit'];
		$order=$tag['order']?$tag['order']:'id desc';
		$tagCode = <<<'CODE'
			$__tag_ad__ = M('AdContent')->where(array(
				"tag" =>{tag}
			))->limit({limit})->order('{order}')->select();
			
CODE;
		$tagValues = array(
				'{tag}' => $_tag,
				'{limit}'=>$limit,
				'{order}'=>$order
		);
		return $this->outTag('__tag_ad__', $tag, $content, $tagCode,$tagValues);
	}
	
	
	// 文章栏目列表
	public function _category($tag, $content) {
		$pid = empty($tag['pid']) ? 0 : $tag['pid'];
		$order = empty($tag['order']) ? 'sort' : $tag['order'];
		$limit = empty($tag['limit']) ? '' : intval($tag['limit']);
		$tagCode = <<<'CODE'
		$condition = array(
			'status' => 1,
			'is_deleted' => 0,
			'visible' => 0,
			'cid' => $sid,
			'pid' => {pid},
		);
		$__tag_category__ = M('ArticleCategory')->where($condition)->order('{order}')->limit({limit})->select();
		foreach($__tag_category__ as &$v){
			if($v['type'] == 'page') {
				$v['url'] = get_url($sid, 'Site', 'page', 'id=' . $v['id']);
			} elseif($v['type']=='list'){
				if($v['has_child']){
					$v['url'] = get_url($sid, 'Site', 'cate', 'id=' . $v['id']);
				} else {
					$controllerName = "Article"; 
					empty( $v['content_model'] ) or $controllerName = $v['content_model'];
					$v['url'] = get_url($sid, $controllerName, 'list', 'id=' . $v['id']);
				}
			}
		}
CODE;
		$tagValues = array(
				'{pid}' => $pid,
				'{order}' => $order,
				'{limit}' => $limit
		);
	
		return $this->outTag('__tag_category__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 文章列表
	public function _article($tag, $content) {
		$catId = empty($tag['cat']) ? 0 : $tag['cat'];
		$order = empty($tag['order']) ? 'id desc' : $tag['order'];
		$limit = empty($tag['limit']) ? '' : intval($tag['limit']);
		$tagCode = <<<'CODE'
			$map['status'] = 1;
			$map['is_deleted'] = 0;
			$map['cid'] = $sid;
			if({catId} == 0){
				empty($cat_id) or $map['cat_id'] = $cat_id;
			} elseif({catId} > 0){
				$map['cat_id'] = {catId};
			}
			$__tag_article__ = M('Article')->where($map)->order('{order}')->limit({limit})->select();
			foreach($__tag_article__ as &$v){
				$v['url'] = get_url($sid, 'Article', 'show', 'id=' . $v['id']);
			}
CODE;
		$tagValues = array(
				'{catId}' => $catId,
				'{order}' => $order,
				'{limit}' => $limit 
		);
		return $this->outTag('__tag_article__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 文章列表
	public function _link($tag, $content) {
		$catId = empty($tag['cat']) ? 0 : $tag['cat'];
		
		$order = empty($tag['order']) ? '' : $tag['order'];
		$limit = empty($tag['limit']) ? '' : intval($tag['limit']);
		$tagCode = <<<'CODE'
			$map['status'] = 1;
			$map['is_deleted'] = 0;
			$map['cid'] = $sid;
			if({catId} == 0) {
				empty($cat_id) or $map['cat_id'] = $cat_id;
			} elseif({catId} > 0) {
				$map['cat_id'] = {catId};
			}
			$__tag_link__ = M('Link')->where($map)->order('{order}')->limit({limit})->select();
CODE;
		
		$tagValues = array(
				'{catId}' => $catId,
				'{order}' => $order,
				'{limit}' => $limit
		);
	
		return $this->outTag('__tag_link__', $tag, $content, $tagCode, $tagValues);
	}
	public function _linklist($tag,$content)
	{
		$limit=$tag['limit'];
		$cid=$tag['cid'];
		$catid=$tag['cateid'];
		$name=$tag['name'];
		$str='<?php ';
		$str.='$data=M(\'Link\')->where("cat_id='.$catid.' and cid='.$cid.' and status=1 and is_deleted=0")->select();';
		$str.='?>';
		$str .= '<volist name="data" id="'. $name .'">';
		$str .= $content;
		$str .= '</volist>';
		return $str;
	}
	
	// 楼盘扩展信息列表
	public function _houseConfig($tag, $content) {
		$tagCode = <<<'CODE'
				$map['cid'] = $sid;
				$map['status'] = 1;
				$map['is_deleted'] = 0;
				$__tag_house_config__ = M('HouseConfig')->where($map)->order('sort')->select();
				foreach($__tag_house_config__ as &$v){
					$v['url'] = get_url($sid, 'House', 'config', 'id=' . $v['id']);
				}
CODE;
		return $this->outTag('__tag_house_config__', $tag, $content, $tagCode);
	}
	
	// 户型列表
	public function _houseType($tag, $content) {
		$tagCode = <<<'CODE'
				$map['cid'] = $sid;
				$map['status'] = 1;
				$map['is_deleted'] = 0;
				$__tag_house_type__ = M('HouseType')->where($map)->field('content,sort,status,is_deleted',true)->order('sort')->select();
				foreach($__tag_house_type__ as &$v){
					$v['url'] = get_url($sid, 'House', 'pic', 'id=' . $v['id']);
				}
CODE;
		return $this->outTag('__tag_house_type__', $tag, $content, $tagCode);
	}
	
	// 场景列表
	public function _houseScene($tag, $content) {
		$tagCode = $tagCode = <<<'CODE'
				$map['cid'] = $sid;
				$map['status'] = 1;
				$map['is_deleted'] = 0;
				$map['type_id'] = $type_id;
				$__tag_house_scene__ = M('HouseScene')->where($map)->field('sort,status,is_deleted',true)->order('sort')->select();
				foreach($__tag_house_scene__ as &$v){
					$v['url'] = get_url($sid, 'House', 'scene', 'id=' . $v['id']);
				}
CODE;
		return $this->outTag('__tag_house_scene__', $tag, $content, $tagCode);
	}
	
	// 房产像册列表
	public function _housePhoto($tag, $content) {
		$tagCode = <<<'CODE'
				$map['cid'] = $sid;
				$map['status'] = 1;
				$map['is_deleted'] = 0;
				$__tag_house_photo__ = M('HousePhoto')->where($map)->field('status,is_deleted',true)->select();
				foreach($__tag_house_photo__ as &$v){
					$v['url'] = get_url($sid, 'House', 'note', 'id=' . $v['id']);
				}
CODE;
		return $this->outTag('__tag_house_photo__', $tag, $content, $tagCode);
	}
	
	// 房产图片列表
	public function _housePhotoImage($tag, $content) {
		$limit = empty($tag['limit']) ? 0 : intval($tag['limit']);
		$pt = empty($tag['pt']) ? 0 : $tag['pt'];
		$tagCode = <<<'CODE'
				$map['cid'] = $sid;
				$map['status'] = 1;
				$map['is_deleted'] = 0;
				$map['pt_id'] = {pt}?{pt}:$pt_id;
				$__tag_house_photo_image__ = M('HousePhotoImages')->where($map)->field('sort,status,is_deleted',true)->order('id')->limit({limit})->select();
CODE;
		$tagValues = array(
				'{limit}' => $limit,
				'{pt}' => $pt 
		);
		return $this->outTag('__tag_house_photo_image__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 房产轮播图
	public function _houseSlide($tag, $content) {
		$tagCode = <<<'CODE'
			$slideImages = M('Slide')->where(array(
				"cid" => $sid,
				"mid" => array("exp", "=".M("Model")->where("tag='House'")->field("id")->select(false))
			))->getField("data");
			$__tag_house_slide__ = unserialize($slideImages);
			foreach($__tag_house_slide__ as &$v) {
				$v['img'] = url('Api', 'Image', 'show')."?url=".$v['img'];
			}
CODE;
		return $this->outTag('__tag_house_slide__', $tag, $content, $tagCode);
	}
	
	// 一键拨号
	public function _houseDial($tag, $content) {
		$tagCode = <<<'PARSE'
			$__tag_house_dial__ = array();
			$__tag_house_dial__[] = M('Dial')->where(array(
											"cid" => $sid,
											"mid" => array("exp", "=".M("Model")->where("tag='House'")->field("id")->select(false))
										))->find();
PARSE;
		return $this->outTag('__tag_house_dial__', $tag, $content, $tagCode);
	}
	
	// 一键导航
	public function _houseNav($tag, $content) {
		$tagCode = <<<'CODE'
			$navigate = M('Navigate')->where(array(
											"cid" => $sid,
											"mid" => array("exp", "=".M("Model")->where("tag='House'")->field("id")->select(false))
										))->find();
			$navigate['url'] = 'http://api.map.baidu.com/marker?location='.$navigate['lat'].','.$navigate['lng'].'&title='.$navigate['title'].'&content='.$navigate['address'].'&output=html';
			$__tag_house_nav__[] = $navigate;
CODE;
		return $this->outTag('__tag_house_nav__', $tag, $content, $tagCode);
	}
	
	// 像册列表
	public function _photo($tag, $content) {
		$tagCode = <<<'CODE'
				$map['cid'] = $sid;
				$map['status'] = 1;
				$map['is_deleted'] = 0;
				$__tag_photo__ = M('Photo')->where($map)->field('status,is_deleted',true)->select();
				foreach($__tag_photo__ as &$v){
					$v['url'] = get_url($sid, 'Photo', 'note', 'id=' . $v['id']);
				}
CODE;
		return $this->outTag('__tag_photo__', $tag, $content, $tagCode);
	}
	
	// 图片列表
	public function _photoImage($tag, $content) {
		$limit = empty($tag['limit']) ? 0 : intval($tag['limit']);
		$pt = empty($tag['pt']) ? 0 : $tag['pt'];
		$tagCode = <<<'CODE'
				$map['cid'] = $sid;
				$map['status'] = 1;
				$map['is_deleted'] = 0;
				$map['pt_id'] = {pt}?{pt}:$pt_id;
				$__tag_photo_image__ = M('PhotoImages')->where($map)->field('sort,status,is_deleted',true)->order('id')->limit({limit})->select();
CODE;
		$tagValues = array(
				'{limit}' => $limit,
				'{pt}' => $pt 
		);
		return $this->outTag('__tag_photo_image__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 合作商城配置
	public function _shopConfig($tag, $content) {
		$tagCode = <<<'CODE'
				$map['pid'] = $pid;
				$map['status'] = 1;
				$map['is_deleted'] = 0;
				$__tag_product_config__ = M('ProductConfig')->where($map)->order('sort')->select();
				foreach($__tag_product_config__ as &$v){
					$v['url'] = get_url($sid, 'Product', 'config', 'id=' . $v['id']);
				}
CODE;
		return $this->outTag('__tag_product_config__', $tag, $content, $tagCode);
	}
	
	// 品牌推荐
	public function _shopBrandRecommend($tag, $content) {
		$limit = empty($tag['limit']) ? '' : intval($tag['limit']);
		$tagCode = <<<'CODE'
			$__tag_partner_brand__ = M('PartnerBrand a')->join('__PRODUCT_BRAND__ b on a.brand=b.id',"LEFT")->where(array(
				"a.pid" => $pid,
				"a.is_recommend" => 1,
				"b.status" => array('exp', ' is null or b.status=1'),
				"b.is_deleted" => array('exp', ' is null or b.is_deleted=0'),
			))->field('a.id as id,case when a.name is not null then a.name when a.name is null then b.name end as name,case when a.logo is not null then a.logo when a.logo is null then b.logo end as logo')->limit({limit})->select();
			foreach($__tag_partner_brand__ as &$v) {
				$v['url'] = U( 'Shop/list', array('brands' => $v['id'], 'pid' => $pid, 'sid' => $sid ) );
			}
CODE;
		$tagValues = array(
				'{limit}' => $limit);
		return $this->outTag('__tag_partner_brand__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 商品热词
	public function _shopHotWords($tag, $content) {
		$limit = empty($tag['limit']) ? '' : intval($tag['limit']);
		$tagCode = <<<'CODE'
			$__tag_product_hot_words__ = M('ProductHotWords')->where(array(
				"pid" => $pid,
				"status" => 1,
				"is_deleted" => 0,
			))->limit({limit})->select();
			foreach($__tag_product_hot_words__ as &$v) {
				$v['url'] = U( 'Shop/list', array('keywords' => $v['keyword'], 'pid' => $pid, 'sid' => $sid ) );
			}
CODE;
		$tagValues = array(
				'{limit}' => $limit);
		return $this->outTag('__tag_product_hot_words__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 商品栏目列表
	public function _shopCategory($tag, $content) {
		$key = empty($tag['key']) ? null : $tag['key'];
		$cats = empty($tag['cats']) ? "0" : $tag['cats'];
		$pid = empty($tag['pid']) ? "0" : $tag['pid'];
		$limit = empty($tag['limit']) ? '' : intval($tag['limit']);
		$tagCode = <<<'CODE'
		$condition = array(
			'status' => 1,
			'is_deleted' => 0,
			'partner' => $pid,
		);
		if( {cats} ) {
			$condition['id'] = array('in', '{cats}');
		} else {
			{pid} != -1 and $condition['pid'] = {pid}*1;
			$condition['visible'] = 0;
		}
		$__tag_product_category__ = M('ProductCategory')->where($condition)->order('sort')->limit({limit})->select();
		foreach($__tag_product_category__ as &$v) {
			if($v['type'] == 'page') {
				$v['url'] = U('Shop/page', "sid=".$sid."&pid=".$pid."&id=".$v['id']);
			} elseif($v['type'] == 'url') {
				$v['url'] = str_replace('{{sid}}', $sid, $v['url']);
			} elseif($v['type']=='list') {
				if($v['has_child']) {
					$v['url'] = U('Shop/cate', "sid=".$sid."&pid=".$pid."&id=".$v['id']);
				} else {
					$v['url'] = U('Shop/list', "sid=".$sid."&pid=".$pid."&id=".$v['id']);
				}
			}
		}
CODE;
		$tagValues = array(
				'{cats}' => $cats,
				'{pid}' => $pid,
				'{limit}' => $limit,
		);
		return $this->outTag('__tag_product_category__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 还买了
	public function _shopAlsoBought($tag, $content) {
		$productId = empty($tag['product_id']) ? 0 : $tag['product_id'];
		$order = empty($tag['order']) ? '' : $tag['order'];
		$limit = empty($tag['limit']) ? '' : intval($tag['limit']);
		if( !$productId ) {
			return "";
		}
		$tagCode = <<<'CODE'
			$classId = M("ProductGoods")->where("id=%d", {productId})->getField('class_id');
			$__tag_also_bought__ = M("ProductOrderGoods a")->join("__PRODUCT_GOODS__ b on a.product_id=b.id")->field("distinct b.id as product_id,b.*")
														   ->where(array(
																 'b.pid' => $pid,
																 'b.is_deleted' => 0,
																 'b.status' => 1,
															     'b.class_id' => array('eq', $classId),
															     'a.product_id' => array('neq', {productId}),
															))->order('{order}')->limit({limit})->select();
			foreach($__tag_also_bought__ as &$v){
				unset($v['product_id']);
				$v['url'] =  U('Shop/show', "sid=".$sid."&pid=".$pid."&id=".$v['id']);
				$v['thumb_img'] = url('Api', 'Image', 'thumb')."?url=".$v['pic'];
			}
CODE;
		$tagValues = array(
				'{productId}' => $productId,
				'{order}' => $order,
				'{limit}' => $limit,
		);
		return $this->outTag('__tag_also_bought__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 商品列表
	public function _shopGoods($tag, $content) {
		$catId = empty($tag['cat']) ? 0 : $tag['cat'];
		$classId = empty($tag['class']) ? 0 : intval($tag['class']);
		$child = empty($tag['child']) ? 0 : intval($tag['child']);
		$order = empty($tag['order']) ? '' : $tag['order'];
		$limit = empty($tag['limit']) ? '' : intval($tag['limit']);
		$isLow = empty($tag['is_low']) ? 0 : !!$tag['is_low'];
		$tagCode = <<<'CODE'
			$map['status'] = 1;
			$map['is_deleted'] = 0;
			$map['pid'] = $pid;
			if({catId} == 0){
				empty($cat_id) or $map['cat_id'] = $cat_id;
			} elseif({catId} > 0){
				$map['cat_id'] = {catId};
			}
			if($map['cat_id'] and {child}) {
				$curIds = array($map['cat_id']); $catIds = array();
				do {
					$catIds = array_merge($catIds, $curIds);
					$curIds = M('ProductCategory')->where(array('pid' => array('in', $curIds), 'is_deleted' => 0, 'status' => 1))->getField('id', true);
				} while(!empty($curIds));
				$map['cat_id'] = array('in', $catIds);
			}
			if({classId} == 0){
				empty($class_id) or $map['class_id'] = $class_id;
			} elseif({classId} > 0){
				$map['class_id'] = {catId};
			}
			if({isLow}){
				$map['is_low'] = 1;
			}
			$__tag_product_goods__ = M('ProductGoods')->where($map)->order('{order}')->limit({limit})->select();
			foreach($__tag_product_goods__ as &$v){
				$v['url'] =  U('Shop/show', "sid=".$sid."&pid=".$pid."&id=".$v['id']);
				$v['thumb_img'] = url('Api', 'Image', 'thumb')."?url=".$v['pic'];
			}
CODE;
		$tagValues = array(
				'{catId}' => $catId,
				'{classId}' => $classId,
				'{child}' => $child,
				'{order}' => $order,
				'{limit}' => $limit,
				'{isLow}' => $isLow
		);
		return $this->outTag('__tag_product_goods__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 商品轮播图
	public function _shopSlide($tag, $content) {
		$tagCode = <<<'CODE'
			$slideImages = M('Slide')->where(array(
				"pid" => $pid,
				"mid" => array("exp", "=".M("Model")->where("tag='PartnerProduct'")->field("id")->select(false))
			))->getField("data");
			$__tag_product_slide__ = unserialize($slideImages);
			foreach($__tag_product_slide__ as &$v) {
				$v['img'] = url('Api', 'Image', 'show')."?url=".$v['img'];
			}
CODE;
		return $this->outTag('__tag_product_slide__', $tag, $content, $tagCode);
	}

	// 楼盘扩展信息列表
	public function _productConfig($tag, $content) {
		$tagCode = <<<'CODE'
				$map['cid'] = $sid;
				$map['status'] = 1;
				$map['is_deleted'] = 0;
				$__tag_product_config__ = M('ProductConfig')->where($map)->order('sort')->select();
				foreach($__tag_product_config__ as &$v){
					$v['url'] = get_url($sid, 'Product', 'config', 'id=' . $v['id']);
				}
CODE;
		return $this->outTag('__tag_product_config__', $tag, $content, $tagCode);
	}
		
	// 商品栏目列表
	public function _productCategory($tag, $content) {
		$key = empty($tag['key']) ? null : $tag['key'];
		$pid = empty($tag['pid']) ? "0" : $tag['pid'];
		$limit = empty($tag['limit']) ? '' : intval($tag['limit']);
		$tagCode = <<<'CODE'
		$condition = array(
			'status' => 1,
			'is_deleted' => 0,
			'cid' => $sid,
		);
		{pid} != -1 and $condition['pid'] = {pid}*1;
		$__tag_product_category__ = M('ProductCategory')->where($condition)->order('sort')->limit({limit})->select();
		foreach($__tag_product_category__ as &$v){
			if($v['has_child']){
				$v['url'] = get_url($sid, 'Product', 'cate', 'id=' . $v['id']);
			} else {
				$v['url'] = get_url($sid, 'Product', 'list', 'id=' . $v['id']);
			}
		}
CODE;
		$tagValues = array(
				'{pid}' => $pid,
				'{limit}' => $limit,
				'{key}' => $key,
		);
		return $this->outTag('__tag_product_category__', $tag, $content, $tagCode, $tagValues);
	}
	//商品栏目列表
	public function _productCategorylist($tag,$content)
	{
		$limit=$tag['limit'];
		$pid=$tag['pid'];
		($sid=I('sid',0,'intval')) or exit('非法请求');
		$order=$tag['order'];
		$name=$tag['name'];
		$str='<?php ';
		$str.='$data=M(\'ProductCategory\')->where("cid='.$sid.' and status=1 and is_deleted=0")->order($order)->limit($limit)->select();
			   foreach ($data as &$v){
					if($v["has_child"]){
				     $v["url"] = get_url($sid, "Product", "cate", "id=" . $v["id"]);
				 } else {
					$v["url"] = get_url($sid, "Product", "list", "id=" . $v["id"]);
				      }
	          }
		$data=makeListLevel($data,0,pid);
			';
		$str.='?>';
		$str .= '<volist name="data" id="'. $name .'">';
		$str .= $content;
		$str .= '</volist>';
		return $str;
	}
	// 商品列表
	public function _productGoods($tag, $content) {
		$catId = empty($tag['cat']) ? 0 : $tag['cat'];
		$classId = empty($tag['class']) ? 0 : intval($tag['class']);
		$child = empty($tag['child']) ? 0 : intval($tag['child']);
		$order = empty($tag['order']) ? '' : $tag['order'];
		$limit = empty($tag['limit']) ? '' : intval($tag['limit']);
		$isLow = empty($tag['is_low']) ? 0 : !!$tag['is_low'];
		$tagCode = <<<'CODE'
			$map['status'] = 1;
			$map['is_deleted'] = 0;
			$map['cid'] = $sid;
			if({catId} == 0){
				empty($cat_id) or $map['cat_id'] = $cat_id;
			} elseif({catId} > 0){
				$map['cat_id'] = {catId};
			}
			if($map['cat_id'] and {child}) {
				$curIds = array($map['cat_id']); $catIds = array();
				do {
					$catIds = array_merge($catIds, $curIds);
					$curIds = M('ProductCategory')->where(array('pid' => array('in', $curIds), 'is_deleted' => 0, 'status' => 1))->getField('id', true);
				} while(!empty($curIds));
				$map['cat_id'] = array('in', $catIds);		
			}
			if({classId} == 0){
				empty($class_id) or $map['class_id'] = $class_id;
			} elseif({classId} > 0){
				$map['class_id'] = {catId};
			}
			if({isLow}){
				$map['is_low'] = 1;
			}
			$__tag_product_goods__ = M('ProductGoods')->where($map)->order('{order}')->limit({limit})->select();
			foreach($__tag_product_goods__ as &$v){
				$v['url'] = get_url($sid, 'Product', 'show', 'id=' . $v['id']);
				$v['thumb_img'] = url('Api', 'Image', 'thumb')."?url=".$v['pic'];
			}
CODE;
		$tagValues = array(
				'{catId}' => $catId,
				'{classId}' => $classId,
				'{child}' => $child,
				'{order}' => $order,
				'{limit}' => $limit,
				'{isLow}' => $isLow 
		);
		return $this->outTag('__tag_product_goods__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 商品轮播图
	public function _productSlide($tag, $content) {
		$tagCode = <<<'CODE'
			$slideImages = M('Slide')->where(array(
				"cid" => $sid,
				"mid" => array("exp", "=".M("Model")->where("tag='Product'")->field("id")->select(false))
			))->getField("data");
			$__tag_product_slide__ = unserialize($slideImages);
			foreach($__tag_product_slide__ as &$v) {
				$v['img'] = url('Api', 'Image', 'show')."?url=".$v['img'];
			}
CODE;
		return $this->outTag('__tag_product_slide__', $tag, $content, $tagCode);
	}
	
	// 活动列表
	public function _activity($tag, $content) {
		$tagCode = <<<'CODE'
			$__tag_activity__ = M('Activity this0')->join("__ACTIVITY_CLASS__ this1 on this0.class_id=this1.id", "LEFT")->where(array(
											"this0.cid" => $sid,
											"this0.status" => 1,
											"this0.is_deleted" => 0,
										))->field("this0.id,this0.name,this0.pic,this1.name as className,this1.tag as classTag")->select();
			foreach($__tag_activity__ as &$v) {
				$v['url'] = url('Home', 'Activity', 'show')."?id=".$v[id]."&sid=".$sid;
				// $v['prize_config'] = unserialize($v['prize_config']);
			}
CODE;
		return $this->outTag('__tag_activity__', $tag, $content, $tagCode);
	}
	
	// 商品轮播图
	public function _disShopSlide($tag, $content) {
		$tagCode = <<<'CODE'
			$slideImages = M('Slide')->where(array(
				"cid" => $sid,
				"mid" => array("exp", "=".M("Model")->where("tag='DistributionShop'")->field("id")->select(false))
			))->getField("data");
			$__tag_product_slide__ = unserialize($slideImages);
CODE;
		return $this->outTag('__tag_product_slide__', $tag, $content, $tagCode);
	}


//	// 商品轮播图
//	public function _disShopSlide($tag, $content) {
//		$tagCode = <<<'CODE'
//			$slideImages = M('Slide')->where(array(
//				"cid" => $sid,
//				"mid" => array("exp", "=".M("Model")->where("tag='DistributionShop'")->field("id")->select(false))
//			))->getField("data");
//			$__tag_product_slide__ = unserialize($slideImages);
//			foreach($__tag_product_slide__ as &$v) {
//				$v['img'] = url('Api', 'Image', 'show')."?url=".$v['img'];
//			}
//CODE;
//		return $this->outTag('__tag_product_slide__', $tag, $content, $tagCode);
//	}
	// 商品轮播图
	public function _disShopAd($tag, $content) {
	    $tagCode = <<<'CODE'
			$adLists = M('DistributionShopAd')->where(array(
				"cid" => $sid,
	            "status" => 1,
	            "is_deleted" => 0,
			))->select();
			$__tag_product_ad__ = $adLists;
CODE;
	    return $this->outTag('__tag_product_ad__', $tag, $content, $tagCode);
	}
	
	// 商品栏目列表
	public function _disShopCategory($tag, $content) {
	    $key = empty($tag['key']) ? null : $tag['key'];
	    $cats = empty($tag['cats']) ? "0" : $tag['cats'];
	    $limit = empty($tag['limit']) ? '' : intval($tag['limit']);
	    $tagCode = <<<'CODE'
		$condition = array(
			'status' => 1,
			'is_deleted' => 0,
	        'cid' => $sid,
		);
		if( {cats} ) {
			$condition['id'] = array('in', '{cats}');
		} else {
			$condition['visible'] = 0;
		}
		$__tag_product_category__ = M('DistributionShopCategory')->where($condition)->order('sort')->limit({limit})->select();
		foreach($__tag_product_category__ as &$v) {
			if($v['type'] == 'page') {
				$v['url'] = U('Shop/page', "sid=".$sid."&id=".$v['id']);
			} elseif($v['type'] == 'url') {
				$v['url'] = str_replace('{{sid}}', $sid, $v['url']);
			} elseif($v['type']=='list') {
				$v['url'] = U('DisShop/list', "sid=".$sid."&id=".$v['id']);
			}
		}
CODE;
	    $tagValues = array(
	        '{cats}' => $cats,
	        '{limit}' => $limit,
	    );
	    return $this->outTag('__tag_product_category__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 分销商城配置
	public function _disShopConfig($tag, $content) {
		$tagCode = <<<'CODE'
				$map['cid'] = $sid;
				$map['status'] = 1;
				$map['is_deleted'] = 0;
				$__tag_distribution_shop_config__ = M('DistributionShopConfig')->where($map)->order('sort')->select();
				foreach($__tag_distribution_shop_config__ as &$v){
					$v['url'] = get_url($sid, 'DisShop', 'config', 'id=' . $v['id']);
				}
CODE;
		return $this->outTag('__tag_distribution_shop_config__', $tag, $content, $tagCode);
	}
	
	// 商品热词
	public function _disShopHotWords($tag, $content) {
	    $limit = empty($tag['limit']) ? '' : intval($tag['limit']);
	    $tagCode = <<<'CODE'
			$__tag_product_hot_words__ = M('DistributionShopHotWords')->where(array(
				"cid" => $sid,
				"status" => 1,
				"is_deleted" => 0,
			))->limit({limit})->select();
			foreach($__tag_product_hot_words__ as &$v) {
				$v['url'] = U( 'DisShop/list', array('keywords' => $v['keyword'], 'sid' => $sid ) );
			}
CODE;
	    $tagValues = array(
	        '{limit}' => $limit);
	    return $this->outTag('__tag_product_hot_words__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 品牌推荐
	public function _disShopBrandRecommend($tag, $content) {
	    $limit = empty($tag['limit']) ? '' : intval($tag['limit']);
	    $tagCode = <<<'CODE'
			$__tag_partner_brand__ = M('CustomerBrand a')->join('__PRODUCT_BRAND__ b on a.brand=b.id',"LEFT")->where(array(
				"a.cid" => $sid,
				"a.is_recommend" => 1,
				"b.status" => array('exp', ' is null or b.status=1'),
				"b.is_deleted" => array('exp', ' is null or b.is_deleted=0'),
			))->field('a.id as id,case when a.name is not null then a.name when a.name is null then b.name end as name,case when a.logo is not null then a.logo when a.logo is null then b.logo end as logo')->limit({limit})->select();
			foreach($__tag_partner_brand__ as &$v) {
				$v['url'] = U( 'DisShop/list', array('brands' => $v['id'], 'sid' => $sid ) );
			}
CODE;
	    $tagValues = array(
	        '{limit}' => $limit);
	    return $this->outTag('__tag_partner_brand__', $tag, $content, $tagCode, $tagValues);
	}
	
	// 商品列表
	public function _disShopGoods($tag, $content) {
	    $catId = empty($tag['cat']) ? 0 : $tag['cat'];
	    $classId = empty($tag['class']) ? 0 : intval($tag['class']);
	    $child = empty($tag['child']) ? 0 : intval($tag['child']);
	    $order = empty($tag['order']) ? '' : $tag['order'];
	    $limit = empty($tag['limit']) ? '' : intval($tag['limit']);
	    $isLow = empty($tag['is_low']) ? 0 : !!$tag['is_low'];
	    $tagCode = <<<'CODE'
			$map['status'] = 1;
			$map['is_deleted'] = 0;
			$map['cid'] = $sid;
			if({catId} == 0){
				empty($cat_id) or $map['cat_id'] = $cat_id;
			} elseif({catId} > 0){
				$map['cat_id'] = {catId};
			}
			if($map['cat_id'] and {child}) {
				$curIds = array($map['cat_id']); $catIds = array();
				do {
					$catIds = array_merge($catIds, $curIds);
					$curIds = M('DistributionShopCategory')->where(array('pid' => array('in', $curIds), 'is_deleted' => 0, 'status' => 1))->getField('id', true);
				} while(!empty($curIds));
				$map['cat_id'] = array('in', $catIds);
			}
			if({classId} == 0){
				empty($class_id) or $map['class_id'] = $class_id;
			} elseif({classId} > 0){
				$map['class_id'] = {catId};
			}
			if({isLow}){
				$map['is_low'] = 1;
			}
			$__tag_product_goods__ = M('DistributionShopGoods')->where($map)->order('{order}')->limit({limit})->select();
			foreach($__tag_product_goods__ as &$v){
	            $v['images'] = unserialize($v['images']);
				$v['url'] =  U('DisShop/show', "sid=".$sid."&id=".$v['id']);
				$v['thumb_img'] = url('Api', 'Image', 'thumb')."?url=".$v['images'][0];
			}
CODE;
	    $tagValues = array(
	        '{catId}' => $catId,
	        '{classId}' => $classId,
	        '{child}' => $child,
	        '{order}' => $order,
	        '{limit}' => $limit,
	        '{isLow}' => $isLow
	    );
	    return $this->outTag('__tag_product_goods__', $tag, $content, $tagCode, $tagValues);
	}
	
	/**
	 * 解析并输出标签
	 */
	private function outTag($name, $tag, $content, $tagCode, $tagValues = array()) {
		$_trace = empty($tag['_trace']) ? 0 : 1;
		$tagCode = <<<TAG
<?php
	$tagCode
	$_trace && dump(\${$name});
	\$listSize = count(\${$name});
?><volist name="$name" id="{$tag['id']}" key="{key}">$content</volist>
TAG;
		$tagValues['{key}'] or $tagValues['{key}'] = "index";
		$tagCode = str_replace(array_keys($tagValues), array_values($tagValues), $tagCode);
		return $tagCode;
	}
}